package com.qall.util;


import org.springframework.beans.BeansException;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.boot.origin.OriginTrackedValue;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.StandardServletEnvironment;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Component
public class ApplicationContextProviderUtils implements ApplicationContextAware {
    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

    private static Environment environment;


    private static Map<String, Object> allProperties;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextProviderUtils.applicationContext = applicationContext;
        environment = applicationContext.getEnvironment();
    }

    /**
     * 获取Spring上下文
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取Bean
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

    /**
     * 获取application.properties 中的默认配置
     *
     * @return
     * @Return 配置信息用map的方式返回
     */
    public static Map<String, Object> getDefaultProperties() {
        if (allProperties != null) {
            return allProperties;
        }
        StandardServletEnvironment standardServletEnvironment = (StandardServletEnvironment) environment;
        Map<String, Object> map = new HashMap<>(8);
        Iterator<PropertySource<?>> iterator = standardServletEnvironment.getPropertySources().iterator();
        while (iterator.hasNext()) {
            PropertySource<?> source = iterator.next();
            if (source instanceof OriginTrackedMapPropertySource) {
                Object o = source.getSource();
                if (o instanceof Map) {
                    for (Map.Entry<String, OriginTrackedValue> entry : ((Map<String, OriginTrackedValue>) o).entrySet()) {
                        map.put(entry.getKey(), entry.getValue().getValue());
                    }

                }
            }
        }
        allProperties = map;
        return allProperties;
    }

    public static Environment getEnvironment() {
        return environment;
    }

}
